package be.phl.obstacleapp;

import java.util.ArrayList;

import be.phl.layoutAdapter.UsedAidAdapter;
import be.phl.model.DBAdapterPatient;
import android.app.Fragment;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.RelativeLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.Toast;
import android.widget.TableLayout.LayoutParams;
import android.widget.TextView;

public class UsedAidFragment extends Fragment {

	private RelativeLayout layoutMiddle = null;
	private TableLayout layout1;
	private CheckBox chk;
	private TextView txt;
	private int PatientId;
	private int usedaidid;
	private boolean selected = false;
	int i;
	ArrayList<UsedAidAdapter> usedaids = new ArrayList<UsedAidAdapter>();
	ArrayList<UsedAidAdapter> usedaiddelete = new ArrayList<UsedAidAdapter>();
	Button btnSave;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstances) {
		return inflater.inflate(R.layout.fragment_used_aid, container, false);
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		layoutMiddle = (RelativeLayout) getActivity().findViewById(
				R.id.loophulpmiddel);
		layout1 = (TableLayout) getActivity().findViewById(
				R.id.loophulpmiddeltable);
		btnSave = (Button) getActivity().findViewById(R.id.btnSave);

		btnSave.setOnClickListener(btnOnClickListener);

		PatientId = (int) getActivity().getIntent().getIntExtra("PatientId", 0);
		DBAdapterPatient db = new DBAdapterPatient(getActivity());

		db.open();
		Cursor p = db.getUsedAid();

		while (p.moveToNext()) {

			if (p.getInt(0) > 1) {
				Cursor c = db.getPatientUsedAid(PatientId, p.getInt(0));
				while (c.moveToNext()) {
					if (c.getInt(1) == p.getInt(0)) {
						setSelected(true);
					}
				}
				UsedAidAdapter usedaid = new UsedAidAdapter(p.getInt(0),
						p.getString(1), p.getString(2), selected);
				usedaids.add(usedaid);

				UsedAidAdapter usedaiddeleten = new UsedAidAdapter(p.getInt(0),
						p.getString(1), p.getString(2), selected);
				usedaiddelete.add(usedaiddeleten);

				setSelected(false);
			}
		}
		db.close();

		for (final UsedAidAdapter usedaid : usedaids) {
			TableRow tr = new TableRow(getActivity());

			chk = new CheckBox(getActivity());
			chk.setId(usedaid.getId());
			chk.setText(usedaid.getName());
			chk.setTextColor(Color.BLACK);

			if (usedaid.isSelected()) {
				chk.setChecked(true);
			} else {
				chk.setChecked(false);
			}

			chk.setOnCheckedChangeListener(new OnCheckedChangeListener() {

				@Override
				public void onCheckedChanged(CompoundButton buttonView,
						boolean isChecked) {
					if (usedaid.isSelected()) {
						Context context = getActivity().getApplicationContext();
						CharSequence text = "Loophulpmiddel "
								+ usedaid.getName()
								+ " is niet langer geselecteerd";
						int duration = Toast.LENGTH_SHORT;

						Toast toast = Toast.makeText(context, text, duration);
						toast.show();
						usedaid.setSelected(false);
					} else {
						Context context = getActivity().getApplicationContext();
						CharSequence text = "Loophulpmiddel "
								+ usedaid.getName() + " is geselecteerd";
						int duration = Toast.LENGTH_SHORT;

						Toast toast = Toast.makeText(context, text, duration);
						toast.show();
						usedaid.setSelected(true);
					}

				}
			});

			tr.addView(chk);

			txt = new TextView(getActivity());
			txt.setId(i);
			txt.setText(usedaid.getType() == null ? "" : " (" + usedaid.getType() + ")");
			txt.setTextColor(Color.BLACK);
			tr.addView(txt);

			i++;
			RelativeLayout.LayoutParams lay = new RelativeLayout.LayoutParams(
					android.view.ViewGroup.LayoutParams.MATCH_PARENT,
					android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
			lay.addRule(RelativeLayout.BELOW, layoutMiddle.getId());
			layout1.addView(tr, lay);
		}
	}

	Button.OnClickListener btnOnClickListener = new Button.OnClickListener() {
		@Override
		public void onClick(View v) {

			PatientId = (int) getActivity().getIntent().getIntExtra(
					"PatientId", 0);
			DBAdapterPatient db = new DBAdapterPatient(getActivity());
			db.open();

			Cursor ua = db.getUsedAidNone(PatientId);

			while (ua.moveToNext()) {
				setUsedaidid(ua.getInt(0));
			}

			db.updatePatientUsedAid(PatientId, getUsedaidid());

			for (final UsedAidAdapter usedaid : usedaiddelete) {
				if (usedaid.isSelected()) {
					db.deletePatientUsedAids(PatientId, usedaid.getId());
				}
			}

			for (final UsedAidAdapter usedaid : usedaids) {
				if (usedaid.isSelected()) {

					if (db.insertPatientUsedAids(PatientId, usedaid.getId()) > 0) {
						Context context = getActivity().getApplicationContext();
						CharSequence text = "Loophulpmiddel "
								+ usedaid.getName()
								+ " is succesvol toegevoegd";
						int duration = Toast.LENGTH_SHORT;

						Toast toast = Toast.makeText(context, text, duration);
						toast.show();

					} else {

						Context context = getActivity().getApplicationContext();
						CharSequence text = "Loophulpmiddel "
								+ usedaid.getName()
								+ " kon niet toegevoegd worden";
						int duration = Toast.LENGTH_SHORT;

						Toast toast = Toast.makeText(context, text, duration);
						toast.show();
					}
				}
			}

			db.close();
		}
	};

	public boolean isSelected() {
		return selected;
	}

	public void setSelected(boolean selected) {
		this.selected = selected;
	}

	public int getUsedaidid() {
		return usedaidid;
	}

	public void setUsedaidid(int usedaidid) {
		this.usedaidid = usedaidid;
	}
}
